import { ajax, tools } from './ajax.js';

//#region 获取登录用户信息

//登录用户信息
let tbUser;
let tbUserInfo;
let userOtherInfo;

function queryUserInfo() {
  ajax.post('/user/auth/getUserInfo', {}, (data) => {
    console.log(data);
    if (data.success) {
      tbUser = data.tbUser;
      tbUserInfo = data.tbUserInfo;
      userOtherInfo = data.userOtherInfo;
      showUserInfo();
      changeMode(1);
    } else {
      showToast('请先登录');
      changeMode(2);
    }
  });
}

queryUserInfo();

//#endregion

//#region 显示用户登录信息
let divTops = document.querySelectorAll('#divTop > div');
console.log(divTops);

function changeMode(mode) {
  if (mode == 2) {
    divTops[0].style.display = 'none';
    divTops[1].style.display = 'block';
    divTops[2].style.display = 'none';
  } else {
    divTops[0].style.display = 'block';
    divTops[1].style.display = 'none';
    divTops[2].style.display = 'block';
  }
}

changeMode(2);
let btnUser = document.getElementById('btnUser');
let auser = document.getElementById('auser');
let imglogo = document.getElementById('imglogo');

let sexinfo = {
  m: '男生',
  f: '女生',
  n: '保密',
};

function showUserInfo() {
  let sex = sexinfo[tbUserInfo.sex];
  btnUser.innerHTML = `${tbUser.username}`;
  auser.innerHTML = `${tbUser.nickname}(${sex})`;
  imglogo.setAttribute('src', tbUserInfo.img);
}

//#endregion

//#region 安全退出
let alogout = document.getElementById('alogout');

alogout.addEventListener('click', () => {
  ajax.post('/user/auth/logout', {}, (data) => {
    location.href = 'login.html';
  });
});

//#endregion

//#region 修改密码
let apwd = document.getElementById('apwd');
let dialogPwd = document.getElementById('dialogPwd');
let txt_oldpwd = document.getElementById('txt_oldpwd');
let txt_password = document.getElementById('txt_password');
let btnPwd = document.getElementById('btnPwd');

let dpwd = new bootstrap.Modal(dialogPwd);

apwd.addEventListener('click', () => {
  dpwd.show();
});

btnPwd.addEventListener('click', () => {
  let info = {
    oldpwd: tools.md5(txt_oldpwd.value),
    password: tools.md5(txt_password.value),
  };
  ajax.post('/user/auth/updatePwd', info, (data) => {
    showToast(data.message);
  });
});

//#endregion

//#region 修改附加信息
let amodify = document.getElementById('amodify');
let dialogInfo = document.getElementById('dialogInfo');
let txt_nickname = document.getElementById('txt_nickname');
let txt_img = document.getElementById('txt_img');
let txt_info = document.getElementById('txt_info');
let txt_qq = document.getElementById('txt_qq');
let txt_wechat = document.getElementById('txt_wechat');
let sexm = document.getElementById('sexm');
let sexf = document.getElementById('sexf');
let sexn = document.getElementById('sexn');
let btnInfo = document.getElementById('btnInfo');

let dinfo = new bootstrap.Modal(dialogInfo);

amodify.addEventListener('click', () => {
  txt_nickname.value = tbUser.nickname;
  txt_img.value = tbUserInfo.img;
  txt_info.value = tbUserInfo.info;
  txt_qq.value = tbUserInfo.qq;
  txt_wechat.value = tbUserInfo.wechat;
  if (tbUserInfo.sex == 'm') {
    sexm.checked = true;
  } else if (tbUserInfo.sex == 'f') {
    sexf.checked = true;
  } else {
    sexn.checked = true;
  }

  dinfo.show();
});

btnInfo.addEventListener('click', () => {
  let info = {
    nickname: txt_nickname.value,
    img: txt_img.value,
    info: txt_info.value,
    qq: txt_qq.value,
    wechat: txt_wechat.value,
  };
  if (sexm.checked) {
    info.sex = 'm';
  } else if (sexf.checked) {
    info.sex = 'f';
  } else {
    info.sex = 'n';
  }
  ajax.post('/user/auth/updateUserInfo', info, (data) => {
    showToast(data.message);
    if (data.success) {
      queryUserInfo();
    }
  });
});
//#endregion

//#region 绑定邮箱的部分
let aemail = document.getElementById('aemail');
let dialogEmail = document.getElementById('dialogEamil');
let txt_email = document.getElementById('txt_email');
let txt_email_code = document.getElementById('txt_email_code');
let btnSendEamil = document.getElementById('btnSendEamil');
let btnEmail = document.getElementById('btnEmail');

let edialog = new bootstrap.Modal(dialogEmail);

aemail.addEventListener('click', () => {
  txt_email.value = tbUserInfo.email;
  edialog.show();
});

btnSendEamil.addEventListener('click', () => {
  ajax.post(
    '/tool/sendEmailCode',
    {
      email: 'txt_email.value',
    },
    (data) => {
      showToast(data.message);
    }
  );
});

btnEmail.addEventListener('click', () => {
  ajax.post(
    '/user/auth/updateUserEmail',
    {
      email: txt_email.value,
      code: txt_email_code.value,
    },
    (data) => {
      showToast(data.message);
      if (data.success) {
        queryUserInfo();
      }
    }
  );
});

//#endregion

//#region 弹出层提升框

let liveToast = document.getElementById('liveToast');
const toast = new bootstrap.Toast(liveToast);
//#liveToast . toast-body
//querySelector是通过css选择器获取页面上的元素（只会返回一个）
const toastBody = document.querySelector('#liveToast .toast-body');
console.log('toastBady===>', toastBody);
function showToast(message) {
  toastBody.innerHTML = message;
  toast.show();
}
//#endregion
